package com.xw.bo.ks.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xw.bo.ks.entity.CacheControllerInfo;
import com.xw.bo.ks.entity.base.BoPage;
import com.xw.bo.ks.entity.base.HeadDataPage;
import com.xw.bo.ks.entity.bean.CacheControllerInfoQo;
import com.xw.bo.ks.entity.bean.CacheControllerInfoReq;
import com.xw.bo.ks.mapper.CacheControllerInfoMapper;
import com.xw.bo.ks.service.CacheControllerInfoService;
import com.xw.bo.ks.util.XwUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author： xw ftl auto
 * @create： 2024-06-06 17:49:45
 */
@Service
public class CacheControllerInfoServiceImpl extends ServiceImpl<CacheControllerInfoMapper, CacheControllerInfo> implements CacheControllerInfoService {

    @Resource
    private CacheControllerInfoMapper cacheControllerInfoMapper;

    @Override
    public BoPage<CacheControllerInfo> pageSampleCacheControllerInfo(Integer pageNumber, Integer pageSize) {

        HeadDataPage headDataPage = new HeadDataPage();
        headDataPage.setPageNumber(pageNumber);
        headDataPage.setPageSize(pageSize);

        return boPageBean(new QueryWrapper<>(), headDataPage);
    }

    @Override
    public BoPage<CacheControllerInfo> pageEqCacheControllerInfoDto(CacheControllerInfoReq cacheControllerInfoReq) {

        CacheControllerInfo queryCacheControllerInfo = new CacheControllerInfo();
        BeanUtils.copyProperties(cacheControllerInfoReq, queryCacheControllerInfo);

        LambdaQueryWrapper<CacheControllerInfo> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(null != cacheControllerInfoReq.getCacheControllerInfoId(),CacheControllerInfo::getId, cacheControllerInfoReq.getCacheControllerInfoId());
        queryWrapper.eq(null != cacheControllerInfoReq.getUri(),CacheControllerInfo::getUri, cacheControllerInfoReq.getUri());
        queryWrapper.eq(null != cacheControllerInfoReq.getName(),CacheControllerInfo::getName, cacheControllerInfoReq.getName());
        queryWrapper.eq(null != cacheControllerInfoReq.getEnableType(),CacheControllerInfo::getEnableType, cacheControllerInfoReq.getEnableType());
        queryWrapper.eq(null != cacheControllerInfoReq.getCacheTime(),CacheControllerInfo::getCacheTime, cacheControllerInfoReq.getCacheTime());
        queryWrapper.eq(null != cacheControllerInfoReq.getIsDelete(),CacheControllerInfo::getIsDelete, cacheControllerInfoReq.getIsDelete());
        queryWrapper.eq(null != cacheControllerInfoReq.getDeletedAt(),CacheControllerInfo::getDeletedAt, cacheControllerInfoReq.getDeletedAt());
        queryWrapper.eq(null != cacheControllerInfoReq.getCreatedAt(),CacheControllerInfo::getCreatedAt, cacheControllerInfoReq.getCreatedAt());
        queryWrapper.eq(null != cacheControllerInfoReq.getUpdatedAt(),CacheControllerInfo::getUpdatedAt, cacheControllerInfoReq.getUpdatedAt());
        queryWrapper.eq(null != cacheControllerInfoReq.getCreatedYmd(),CacheControllerInfo::getCreatedYmd, cacheControllerInfoReq.getCreatedYmd());

        if (StringUtils.isNotEmpty(cacheControllerInfoReq.getStartTime())) {
            Integer startTime = XwUtil.parseToInteger(cacheControllerInfoReq.getStartTime());
            queryWrapper.ge(CacheControllerInfo::getCreatedAt, startTime);
        }
        if (StringUtils.isNotEmpty(cacheControllerInfoReq.getEndTime())) {
            Integer endTime = XwUtil.parseToInteger(cacheControllerInfoReq.getEndTime());
            queryWrapper.le(CacheControllerInfo::getCreatedAt, endTime);
        }

        queryWrapper.orderByDesc(CacheControllerInfo::getCreatedAt);

        return boPageBean(queryWrapper, cacheControllerInfoReq);
    }


    @Override
    public BoPage<CacheControllerInfo> pageCacheControllerInfoMapper(CacheControllerInfoReq cacheControllerInfoReq) {
        if (!"desc".equals(cacheControllerInfoReq.getOrderValue()) && !"aes".equals(cacheControllerInfoReq.getOrderValue())) {
            cacheControllerInfoReq.setOrderValue("desc");
        }

        CacheControllerInfoQo cacheControllerInfoQo = new CacheControllerInfoQo();
        BeanUtils.copyProperties(cacheControllerInfoReq,cacheControllerInfoQo);

        if (cacheControllerInfoReq.getCacheControllerInfoId() != null){
            cacheControllerInfoQo.setId(cacheControllerInfoReq.getCacheControllerInfoId());
        }

        if (StringUtils.isNotEmpty(cacheControllerInfoReq.getStartTime())) {
            Integer startTime = XwUtil.parseToInteger(cacheControllerInfoReq.getStartTime());
            cacheControllerInfoQo.setStartTime(startTime);
        }
        if (StringUtils.isNotEmpty(cacheControllerInfoReq.getEndTime())) {
            Integer endTime = XwUtil.parseToInteger(cacheControllerInfoReq.getEndTime());
            cacheControllerInfoQo.setEndTime(endTime);
        }

        return boPageBeanBack(new QueryWrapper<>(), cacheControllerInfoReq, wrapper -> cacheControllerInfoMapper.pageCacheControllerInfoMapper(cacheControllerInfoQo));
    }


}
